package com.autohammal.ui.frame;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.wb.swt.SWTResourceManager;

public class Main {

	protected Shell hammalMainShell, hammalConfigShell;
	private CTabFolder tabFolderMonitor;

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			Main window = new Main();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		hammalMainShell.open();
		hammalMainShell.layout();
		while (!hammalMainShell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		hammalMainShell = new Shell();
		hammalMainShell.setImage(SWTResourceManager.getImage(Main.class, "/com/autohammal/image/kcron.ico"));
		hammalMainShell.setSize(400, 160);
		hammalMainShell.setText("Auto-Hammal");
		hammalMainShell.setLayout(new FillLayout(SWT.HORIZONTAL));

		tabFolderMonitor = new CTabFolder(hammalMainShell, SWT.BORDER
				| SWT.BOTTOM);
		tabFolderMonitor.setDragDetect(false);
		tabFolderMonitor.setSimple(false);
		final Menu menuOptions = new Menu(tabFolderMonitor);
		tabFolderMonitor.setMenu(menuOptions);

		MenuItem mntmEdit = new MenuItem(menuOptions, SWT.NONE);
		mntmEdit.setText("Edit");

		new MenuItem(menuOptions, SWT.SEPARATOR);

		MenuItem mntmCopy = new MenuItem(menuOptions, SWT.NONE);
		mntmCopy.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				createTabItem(tabFolderMonitor);
			}
		});
		mntmCopy.setText("New");

		MenuItem mntmCopyNew = new MenuItem(menuOptions, SWT.NONE);
		mntmCopyNew.setText("Copy New");

		new MenuItem(menuOptions, SWT.SEPARATOR);

		MenuItem mntmDelete = new MenuItem(menuOptions, SWT.NONE);
		mntmDelete.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				deleteTabItem(tabFolderMonitor);
			}
		});
		mntmDelete.setText("Delete");

		tabFolderMonitor.setSelectionBackground(Display.getCurrent()
				.getSystemColor(SWT.COLOR_TITLE_INACTIVE_BACKGROUND_GRADIENT));

		CTabItem tbtmWelcome = new CTabItem(tabFolderMonitor, SWT.NONE);
		tbtmWelcome.setText("Welcome");
		Composite composite = new Composite(tabFolderMonitor, SWT.NONE);
		tbtmWelcome.setShowClose(true);
		tbtmWelcome.setControl(composite);

		tabFolderMonitor.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				createTabItem(tabFolderMonitor);
			}
		});
	}

	protected void deleteTabItem(final CTabFolder tabFolderMonitor) {
		CTabItem tabItem = tabFolderMonitor.getSelection();
		tabItem.dispose();
	}

	private void createTabItem(final CTabFolder tabFolderMonitor) {
		CTabItem tbtmNewItem = new CTabItem(tabFolderMonitor, SWT.NONE);
		Composite composite = new Composite(tabFolderMonitor, SWT.NONE);
		tbtmNewItem.setControl(composite);
		tbtmNewItem.setText("New Item");
		tabFolderMonitor.setSelection(tbtmNewItem);
		getConfigShell().open();
	}

	private void createConfigContents() {
		hammalConfigShell = new Shell();
		hammalConfigShell.setSize(300, 380);
		hammalConfigShell.setImage(SWTResourceManager.getImage(Main.class,
				"/com/autohammal/image/kcron.ico"));
		hammalConfigShell.setLayout(new FillLayout(SWT.HORIZONTAL));
		hammalConfigShell.setText("Monitor Configuration");
		new HammalConfigComposite2(hammalConfigShell, SWT.NONE);

		// new Test(hammalConfigShell, SWT.NONE);
	}

	protected Shell getConfigShell() {
		if (hammalConfigShell == null || hammalConfigShell.isDisposed()) {
			createConfigContents();
		}
		return hammalConfigShell;
	}

	public CTabFolder getTabFolderMonitor() {
		return tabFolderMonitor;
	}
}
